#!/bin/sh

die () { echo "$@" ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

check () {
  OUTPUT="$(nvm_filter_min_versions "$1" "$2")" || die "nvm_filter_min_versions $1 failed for >$2<"
  [ "_${OUTPUT}" = "_$3" ] || die "nvm_filter_min_versions $1: $4; got >${OUTPUT}<, expected >$3<"
}

check 18 "$(printf 'v0.12.18\nv16.20.2\nv17.9.1\nv18.0.0\nv18.20.4\nv20.1.0')" "$(printf 'v18.0.0\nv18.20.4\nv20.1.0')" 'a major minimum drops only older versions, v0.x included'

check 1 "$(printf 'v0.1.14\nv0.12.18\nv1.0.0')" 'v1.0.0' 'v0.x rows are compared, not kept as non-version rows'

check 18.1 "$(printf 'v18.0.9\nv18.1.0\nv18.2.0\nv19.0.0')" "$(printf 'v18.1.0\nv18.2.0\nv19.0.0')" 'a minor minimum compares the minor'

check 18.1.5 "$(printf 'v18.1.4\nv18.1.5\nv18.1.6\nv18.2.0\nv17.99.99')" "$(printf 'v18.1.5\nv18.1.6\nv18.2.0')" 'a full minimum compares the patch, inclusively'

check v18.1.5 "$(printf 'v18.1.4\nv18.1.5')" 'v18.1.5' 'a leading `v` on the minimum is ignored'

check 9 "$(printf 'v8.17.0\nv9.0.0\nv10.0.0\nv100.0.0')" "$(printf 'v9.0.0\nv10.0.0\nv100.0.0')" 'components compare numerically, not as strings'

check 3 "$(printf 'iojs-v1.8.4\niojs-v2.5.0\niojs-v3.0.0\niojs-v3.3.1')" "$(printf 'iojs-v3.0.0\niojs-v3.3.1')" 'io.js rows compare by their version'

check iojs-v3 "$(printf 'v2.0.0\nv3.0.0')" 'v3.0.0' 'an `iojs-v` prefix on the minimum is ignored'

check 27 "$(printf 'v26.9.9\nv27.0.0-rc.1\nv27.0.0')" "$(printf 'v27.0.0-rc.1\nv27.0.0')" 'a partial minimum includes the prereleases of its first release'

check 27.0 "$(printf 'v26.9.9\nv27.0.0-rc.1\nv27.0.0')" "$(printf 'v27.0.0-rc.1\nv27.0.0')" 'a major.minor minimum includes the prereleases of its first release'

check 27.0.0 "$(printf 'v27.0.0-rc.1\nv27.0.0\nv27.0.1-nightly2026')" "$(printf 'v27.0.0\nv27.0.1-nightly2026')" 'a full minimum excludes its own prereleases, but not later ones'

check 27.0.1 "$(printf 'v27.0.1-nightly2026\nv27.0.1')" 'v27.0.1' 'a prerelease is below its release'

check 27 "$(printf 'v26.0.0-rc.1\nv28.0.0-rc.1')" 'v28.0.0-rc.1' 'a prerelease of an older major is dropped'

check 18 "$(printf 'v16.20.2\t(LTS: Gallium)\nv18.20.4\t(Latest LTS: Hydrogen)\n  v17.9.1  *\n  v20.1.0  *')" "$(printf 'v18.20.4\t(Latest LTS: Hydrogen)\n  v20.1.0  *')" 'the version is the first field, even when indented, and kept rows are printed whole'

check 18 "$(printf 'N/A\nv16.0.0\nsystem\n\nnode-v1.0.0\nvNext\niojs-vNext\nv18.0.0')" "$(printf 'N/A\nsystem\n\nnode-v1.0.0\nvNext\niojs-vNext\nv18.0.0')" 'rows that are not versions, including `v` or `iojs-v` without a digit, are kept, in order'

check 20 "$(printf 'v22.0.0\nv18.0.0\nv20.0.0')" "$(printf 'v22.0.0\nv20.0.0')" 'the input order is kept'

check 99 "$(printf 'v16.0.0\nv18.0.0')" '' 'every version below the minimum leaves nothing'

check '' "$(printf 'v0.0.0-pre\nv0.1.0\nv18.0.0')" "$(printf 'v0.0.0-pre\nv0.1.0\nv18.0.0')" 'an empty minimum keeps everything'

OUTPUT="$(nvm_filter_min_versions 18; echo x)"
[ "_${OUTPUT}" = "_$(printf '\nx')" ] || die "a missing input should print one empty line; got >${OUTPUT}<"

OUTPUT="$(nvm_filter_min_versions; echo x)"
[ "_${OUTPUT}" = "_$(printf '\nx')" ] || die "no arguments should print one empty line; got >${OUTPUT}<"

OUTPUT="$(nvm_filter_min_versions 18 "$(printf 'v16.0.0\nv18.0.0')" 2>&1)"
[ "_${OUTPUT}" = '_v18.0.0' ] || die "nothing should be printed to stderr; got >${OUTPUT}<"
